<?php 
/**
 * 静态页控制器
 */
class HtmlController extends Controller{
	public function __init(){
		if(!session('aid') || !session('aname')){
			go(U('Admin/Login/index'));
		}
	}
    /**
     * 生成首页静态
     */
    public function index(){
    		$this->_createHtml('Index', './index.html');
		$this->success('生成首页静态成功',U('Admin/Index/welcome'));
    }
	/**
     * 生成列表页静态
     */
    public function _list(){
    		$data = K('Category')->where("is_show=1 AND is_listhtml=1")->all();
		foreach ($data as $v) {
			//设置分页的静态地址
			Page::$staticUrl = __STATIC__ . '/' . C('index_tpl_style') . '/' . $v['htmldir'] . '/{page}.html';
			//生成列表页静态
			$_GET['cid'] = $v['cid'];
			$path = 'Static/' . C('index_tpl_style') . '/' . $v['htmldir'] . '/index.html';
			unset($_GET['page']);
			$this->_createHtml('List', $path);
			
			//生成分页静态
			for ($i=1; $i <= Page::$staticTotalPage; $i++) {
				$_GET['page'] = $i; 
				$path = 'Static/' . C('index_tpl_style') . '/' . $v['htmldir'] . '/' . $i . '.html';
				$this->_createHtml('List', $path);
			}
		}
		$this->success('生成列表页静态成功',U('Admin/Index/welcome'));
    }
	/**
     * 生成内容页静态
     */
    public function content(){
    	$data = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->all();
		foreach ($data as $v) {
			$path = 'Static/' . C('index_tpl_style') . '/' . $v['htmldir'] . '/arc/'.$v['aid'].'.html';
			$this->_createHtml('Content', $path);
		}
		$this->success('生成内容页静态成功',U('Admin/Index/welcome'));
    }
	
	/**
	 * 生成静态
	 */
	private function _createHtml($c,$path){
		//开启缓冲区
		ob_start();
		//访问首页或者列表页或者内容页
		A("Index/{$c}/index");
		$data = ob_get_contents();
		ob_clean();
		//创建目录
		$dir = dirname($path);
		is_dir($dir) || mkdir($dir,0777,true);
		//写入静态文件
		file_put_contents($path, $data);
		
	}
	
 }
 
 
 
 
 
 
 
 
 
 
 




 ?>